const { sendFn, querySql, intercept } = require('../model/send')
const formatTen = require('../model/date')
const ProjectMethod = {
  // 新增
  addProject: async (req, res) => {
    console.log(req.body)
    for (let KEY in req.body) {
      if (req.body[KEY] == "" || !req.body[KEY]) return sendFn(res, 500, `${KEY} 字段必须填写`)
    }
    const resp = await querySql(res,
      `insert into project 
      (project_name,
        project_type,
        project_technologyStack,
        project_difficulty,
        project_address,
        project_img,
        project_remark,
        project_cycle,
        startingTime,
        endTime,
        userid) value(?,?,?,?,?,?,?,?,?,?,?)`,
      [req.body.name, req.body.type,
      req.body.technologyStack,
      req.body.difficulty,
      req.body.address,
      req.body.img,
      req.body.remark,
      req.body.cycle,
      req.body.startTime,
      req.body.endTime,
      req.id])
    if (resp) sendFn(res, 200, '提交成功')
  },


  // 修改项目

  editProject: async (req, res) => {
    let {
      name,
      type,
      technologyStack,
      difficulty,
      address,
      img,
      remark,
      cycle,
      startingTime,
      endTime,
      projectCategoryid
    } = req.body
    const resT = await querySql(res, `
    UPDATE project 
    SET project_name = ${name},
    project_type = ${type},
    project_technologyStack = ${technologyStack},
    project_difficulty = ${difficulty},
    project_address = ${address},
    project_img = ${img},
    project_remark = ${remark},
    project_cycle = ${cycle},
    startingTime = ${startingTime},
    endTime = ${endTime} 
    WHERE
	  projectCategoryid = ${projectCategoryid}`)

  },
  // 获取项目大类
  getProject: async (req, res,) => {
    // console.log(req.query)
    if (!req.query.pageNo) return sendFn(res, 500, '请传参数pageNo')
    if (!req.query.pageSize) return sendFn(res, 500, '请传参数pageSize')
    const resT = await querySql(res, `SELECT * FROM project WHERE userid = ${req.id}`,)
    const resp = await querySql(res, `SELECT * FROM project WHERE userid = ${req.id} ${req.query.type ? `project_type =${req.query.type}` : ``} LIMIT ${req.query.pageSize} offset ${req.query.pageNo - 1}`, true)
    resp.forEach(item => {
      // item.name = item.project_name
      // item.type = item.project_type
      // item.cycle = item.project_cycle

      item.projectImg = item.projectImg ? item.projectImg.split(',') : item.projectImg
      // item.userid = item.userid
      // item.remark = item.project_remark
      // item.address = item.project_address
      // item.difficulty = item.project_difficulty
      // item.endTime = formatTen(item.endTime)
      // item.projectCategoryid = item.projectCategoryid
      item.projectTechnologyStack = item.projectTechnologyStack ? item.projectTechnologyStack.split(',') : item.projectTechnologyStack
    })
    // console.log(resp)
    sendFn(res, 200, '获取列表成功', resp, resT.length)
  },

  // 详情信息
  gitProjectedit: async (req, res) => {
    const resp = await querySql(res, `SELECT * FROM project WHERE project_id = ${req.query.id}`,)
    sendFn(res, 200, '获取详情成功', resp[0],)
  },
  // 获取项目大类 统计
  getProjectNum: async (req, res) => {
    console.log(req.id)
    if (!req.id) return sendFn(res, 1000, '请登录再操作哦')
    const id = toString(req.id)
    if (!id.length) return sendFn(res, 1000,)
    let result = []
    let NumObj = {
      PC: [], H5: [], MP_WEIXIN: [], IOS: [], android: []
    }
    const resp = await querySql(res, `SELECT * FROM project WHERE userid = ${req.id}`)
    await resp.forEach((item) => {
      switch (item.project_type) {
        case '0':
          NumObj.PC.push(item)
          break
        case '1':
          NumObj.H5.push(item)

          break
        case '2':
          NumObj.MP_WEIXIN.push(item)

          break
        case '3':
          NumObj.IOS.push(item)

          break
        case '4':
          NumObj.android.push(item)
          break

      }
    })


    for (KEY in NumObj) {
      result.push({ value: NumObj[KEY].length, name: KEY })
    }
    sendFn(res, 200, '获取成功', result)

  },
}

module.exports = ProjectMethod
